package com.hp.easyBuy.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hp.easyBuy.dao.CartDao;
import com.hp.easyBuy.entity.TCart;
import com.hp.easyBuy.entity.DTO.TCartDto;
import com.hp.easyBuy.entity.TOrder;
import com.hp.easyBuy.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service("cartService")
public class CartServiceImpl extends ServiceImpl<CartDao, TCart> implements CartService {

    @Autowired
    private CartDao cartDao;


    @Override
    public List<TCartDto> getCartList(Long userid) {
        QueryWrapper<TCartDto> wrapper = new QueryWrapper<>();
        wrapper.eq("c.USERID",userid);

        return cartDao.queryAllCart(wrapper);
    }

    @Override
    public boolean updateCartNum(Long cartid, Long num) {
        //先通过id获取实体类
        TCart cart = cartDao.selectById(cartid);
        //再更新实体类中的num值
        cart.setNum(num);
        //更新整个实体类
        return cartDao.updateById(cart)>0;
    }

    
        @Override
    public boolean addCart(Integer goodsid, Long userid) {
        //首先判断商品是否已经存在用户的购物车
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("GOODSID", goodsid);
        queryWrapper.eq("USERID", userid);
        TCart tCart = cartDao.selectOne(queryWrapper);
        if(tCart != null){
            //如果该用户的购物车已经存在该商品则将该商品的数量加1
            tCart.setNum(tCart.getNum()+1);
            return cartDao.updateById(tCart) > 0;
        }else{
            //否则增加一个商品
            TCart newCart = new TCart(userid,goodsid,1);
            return cartDao.insert(newCart) > 0;
        }
    }






}
